import { _decorator, Component, instantiate, Node, Prefab, math, log,macro } from 'cc';
import { Bullet } from '../bullet/Bullet';
import { EnemyPlane } from '../plane/EnemyPlan';
import { Contant } from './Contant';
import { BulletProp } from '../bullet/BulletProp';
const { ccclass, property } = _decorator;

@ccclass('gameManger')
export class gameManger extends Component {

    // 飞机对象
    @property(Node)
    public airPlayer: Node = null  
    // 子弹对象
    @property(Prefab)
    public bullet01: Prefab = null
    @property(Prefab)
    public bullet02: Prefab = null 
    @property(Prefab)
    public bullet03: Prefab = null 
    @property(Prefab)
    public bullet04: Prefab = null 
    @property(Prefab)
    public bullet05: Prefab = null 

    // 敌机
    @property(Prefab)
    public enemy01: Prefab = null
    @property(Prefab)
    public enemy02: Prefab = null
    @property
    public enemy1Speed = 0.5;
    @property
    public enemy2Speed = 0.7;
    @property
    public createEnemyTime = 1

    // 子弹奖励
    @property(Prefab)
    public bulletM: Prefab = null
    @property(Prefab)
    public bulletH: Prefab = null
    @property(Prefab)
    public bulletS: Prefab = null
    @property
    public propSpeed = 0.3

    // 子弹管理节点
    @property(Node)
    public bulletManager: Node = null 
    // 射击的周期
    @property
    public shootTime = 0.3
    // 子弹移动的速度
    @property
    public bulletSpeed = 3
    // 当前射击的周期
    private _currShootTime = 0
    // 是否触摸
    private _isShooting = false;

    // 游戏是否结束
    public isStartGame = false

    // 分数
    public _scope = 0

    private _combinationInterval = Contant.Combination.PLAN1;
    private _currCreateEnemyTime = 0
    private _bulletType = Contant.BulletPropType.BULLET_M;

    /**
     * 页面刚进入的时候调用
     */
    start() {
        this._init()
    }

    _init(){
        // 开始就发射一颗
        this._currShootTime = this.bulletSpeed
        // 初始化敌机模式
        this.changePlanMode()
        this.createBulletProp()
    }

    /**
     * 毫秒级调用，每毫秒调用一次
     * @param deltaTime 
     */
    update(deltaTime: number) {
        // 一次发射3颗
        this._currShootTime += deltaTime
        // 必须是触摸，同时不能在发射周期内
        if(this._bulletType = Contant.BulletPropType.BULLET_H){
            if(this._isShooting && this._currShootTime > this.shootTime){
                this.createPlayerBulletTwo()
                this._currShootTime = 0
            }
        }else {
            if(this._isShooting && this._currShootTime > this.shootTime){
                this.createPlayerBullet()
                this._currShootTime = 0
            }
        }
        // 根据敌机组合模式生成飞机
        // 生成一架飞机的模式
        this._currCreateEnemyTime += deltaTime
        if (this._combinationInterval === Contant.Combination.PLAN1) {
            if(this._currCreateEnemyTime > this.createEnemyTime){
                this.createEnemyPlan()
                this._currCreateEnemyTime = 0
            }
        } else if(this._combinationInterval === Contant.Combination.PLAN2) {    // 一次出现5架一字排开
            if(this._currCreateEnemyTime > this.createEnemyTime*2){
                this.createCombinationOne()
                this._currCreateEnemyTime = 0
            }
        }else {
            if(this._currCreateEnemyTime > this.createEnemyTime*2.7){
                this.createCombinationTwo()
                this._currCreateEnemyTime = 0
            }
        }
        if(this._combinationInterval > 2){
            this._combinationInterval = 0
        }
    }




    /**
     * ===================================================界面逻辑==========================================
     * 重新再开一把
     * 1.关闭结算页面
     * 2.重置分数、飞机位置、敌机出现组合、子弹类型
     * 3.关闭定时任务
     */
    reStart() {
        this._currShootTime = 0
        this._isShooting = false;
        this.isStartGame = false
        this._combinationInterval = Contant.Combination.PLAN1;
        this._currCreateEnemyTime = 0
        this._bulletType = Contant.BulletPropType.BULLET_M;
        this.airPlayer.setPosition(0, 0, -0.48)
        this._scope = 0
    }

    /**
     * 返回到首页
     */
    returnMain() {
        this._currShootTime = 0
        this._isShooting = false;
        this.isStartGame = false
        this._combinationInterval = Contant.Combination.PLAN1;
        this._currCreateEnemyTime = 0
        this._bulletType = Contant.BulletPropType.BULLET_M;
        this.unschedule(this._modelChanged)
    }

    public addScope() {
        this._scope++;
    }





    /**
     * ====================================================子弹相关的代码====================================
     * 创建子弹
     */
    createPlayerBullet(){
        console.log("开始创建子弹")
        // 因为是预制所以需要通过实例获取
        const bullet = instantiate(this.bullet01)
        // 需要获取父级节点
        bullet.setParent(this.bulletManager)
        // 设置子弹所在位置，对应飞机的位置
        const airPosition = this.airPlayer.getPosition();
        bullet.setPosition(airPosition.x, airPosition.y, airPosition.z - 9)
        const bulletComp = bullet.getComponent(Bullet)
        bulletComp.bulletSpeed = this.bulletSpeed
    }

    /**
     * 双发子弹
     */
    createPlayerBulletTwo() {
        const airPos = this.airPlayer.getPosition()

        let bullet = instantiate(this.bullet01)
        bullet.setParent(this.bulletManager)
        bullet.setPosition(airPos.x - 2, airPos.y, airPos.z + 9)
        const bullet1 = bullet.getComponent(Bullet)
        bullet1.bulletSpeed = this.bulletSpeed

        let bullet2 = instantiate(this.bullet01)
        bullet2.setParent(this.bulletManager)
        bullet2.setPosition(airPos.x + 2, airPos.y, airPos.z + 9)
        const bulletE2 = bullet2.getComponent(Bullet)
        bulletE2.bulletSpeed = this.bulletSpeed
    }

    /**
     * 切换子弹
     */
    changeBulletType(type: number){
        this._bulletType = type
    }

    /**
     * 创建子弹奖励
     * 飞机吃到奖励后子弹会改变
     */
    createBulletProp() {
        let bulletInit : Prefab = null
        const type =  math.randomRangeInt(0,4)
        console.log("type121",type)
        if(type === Contant.BulletPropType.BULLET_H){
            bulletInit = this.bulletH            
        }else if(type === Contant.BulletPropType.BULLET_M){
            bulletInit = this.bulletM           
        }else{
            bulletInit = this.bulletS            
        }
        const prop = instantiate(bulletInit) 
        prop.setParent(this.node)
        prop.setPosition(15, 0, -40);
        const propCom = prop.getComponent(BulletProp)
        propCom.show(this, -this.propSpeed)
    }



    
    /**
     * =======================================生成敌机队列====================================== 
     * 创建显示敌人飞机
     * @param value 
     */
        createEnemyPlan(){
            const whichEnemy = math.randomRangeInt(1,3);
            let prefab : Prefab = null
            let speed = 0
            if(whichEnemy === Contant.EnemyType.TYPE1){
                prefab = this.enemy01
                speed = this.enemy1Speed
            }else{
                prefab = this.enemy02
                speed = this.enemy2Speed
            }
    
            const enemy = instantiate(prefab)
            enemy.setParent(this.node)
            // 创建出敌机对象
            const enemyComp = enemy.getComponent(EnemyPlane)
            enemyComp.show(speed,this)
    
            // 飞机在X轴出现的位置
            const randomPos = math.randomRangeInt(-26, 25)
            // -50 表示飞机固定在z轴-50的位置出现
            enemy.setPosition(randomPos, 0, -50)
    
        }
    
    /**
     * 一次出现5架飞机排成一排
     */
    createCombinationOne(){
        let enemyArray = []
        for(let i=0; i< 5; i++){
            let enemy01: Prefab = this.enemy01
            // 实例化对象
            const enemy = instantiate(enemy01)
            enemy.setParent(this.node)
            // 获取组件
            const enemyCom = enemy.getComponent(EnemyPlane)
            enemyCom.show(this.enemy1Speed,this)
            enemy.setPosition(-16 + i*8,0,-50)
            enemyArray[i] = enemy
        }
    }
    /**
     * 一次出现7架飞机排成大雁型队列
     */
    createCombinationTwo(){
        const combinationPos = [
            -16, 0, -60,
            -9, 0, -55,
            -4, 0, -50,
            0, 0, -45,
            4, 0, -50,
            9, 0, -55,
            16, 0, -60,
        ];
        for(let i=0; i< combinationPos.length; i++){
            let enemyPlan : Prefab = this.enemy02
            const enemy2 = instantiate(enemyPlan)
            enemy2.setParent(this.node)
            const enemyCom = enemy2.getComponent(EnemyPlane)
            enemyCom.show(this.enemy2Speed,this)
            enemy2.setPosition(combinationPos[i],
                combinationPos[i+1],combinationPos[i+2])
        }
    }




    /**
     * ==================================================定时任务实时执行============================================
     * 定时任务改变敌机类型
     */
    changePlanMode(){
        this.schedule(this._modelChanged, 4, macro.REPEAT_FOREVER);
    }
    _modelChanged(){
        this._combinationInterval++;
        this.createBulletProp();
    }

    // 判断是否再触摸状态
    public isShoot(value: boolean){
        this._isShooting = value
    }

}


